package org.bizming.medicine.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.bizming.base.service.SqlHelper;
import org.bizming.medicine.model.help.MedicineStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

public class MedicineStatusMysqlDao implements MedicineStatusDao {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	private JdbcTemplate jdbcTemplate;

	private SqlHelper sqlHelper;

	private String tableName = "medicinestatus";

	public boolean add(MedicineStatus status) {
		List<Object> values = new ArrayList<Object>();
		String sql = sqlHelper.sqlAdd(status, values, getTableName());
		logger.info(sql);

		Object[] o = values.toArray();
		int affectRow = jdbcTemplate.update(sql, o);

		sqlHelper.clear(values);
		sqlHelper.clear(o);

		if (affectRow > 0) {
			return true;
		} else {
			return false;
		}
	}

	public boolean update(MedicineStatus status) {
		List<Object> values = new ArrayList<Object>();
		String sql = sqlHelper.sqlUpdate(status, values, getTableName());
		logger.info(sql);

		values.add(status.getId());
		Object[] o = values.toArray();
		int affectRow = jdbcTemplate.update(sql, o);

		sqlHelper.clear(values);
		sqlHelper.clear(o);

		if (affectRow > 0) {
			return true;
		} else {
			return false;
		}
	}

	public MedicineStatus get(int id) {

		String sql = sqlHelper.sqlGet(null, getTableName());
		logger.info(sql);

		MedicineStatus status = jdbcTemplate.queryForObject(sql, new MedicineStatusRowMapper(), id);

		return status;
	}

	public boolean remove(int id) {

		String sql = sqlHelper.sqlRemove(getTableName());
		logger.info(sql);

		int affectRow = jdbcTemplate.update(sql, id);
		if (affectRow > 0) {
			return true;
		} else {
			return false;
		}

	}

	public List<MedicineStatus> selectList() {

		String sql = sqlHelper.sqlSelectList(MedicineStatus.class, getTableName());
		logger.info(sql);

		Object[] args = new Object[] { 0, 9999 };

		List<MedicineStatus> rows = jdbcTemplate.query(sql, new MedicineStatusRowMapper(), args);

		sqlHelper.clear(args);

		return rows;
	}

	class MedicineStatusRowMapper implements RowMapper<MedicineStatus> {

		public MedicineStatus mapRow(ResultSet rs, int rowNum) throws SQLException {
			MedicineStatus status = new MedicineStatus();
			status.setId(rs.getInt("id"));
			status.setStatus(rs.getString("status"));
			return status;
		}

	}

	// ===================================
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	public SqlHelper getSqlHelper() {
		return sqlHelper;
	}

	public void setSqlHelper(SqlHelper sqlHelper) {
		this.sqlHelper = sqlHelper;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

}
